import { request } from '@/service/request';

// ==================== 填报模板 ====================

/**
 * 获取填报模板列表
 */
export function fetchFillTemplateList(params?: Api.Dataplatform.FillTemplateSearchParams) {
  return request<Api.Dataplatform.FillTemplateList>({
    url: '/dataplatform/fill/template/list',
    method: 'get',
    params
  });
}

/**
 * 获取填报模板详情
 */
export function fetchFillTemplateDetail(id: number) {
  return request<Api.Dataplatform.FillTemplate>({
    url: `/dataplatform/fill/template/${id}`,
    method: 'get'
  });
}

/**
 * 根据编码获取模板
 */
export function fetchFillTemplateByCode(code: string) {
  return request<Api.Dataplatform.FillTemplate>({
    url: `/dataplatform/fill/template/code/${code}`,
    method: 'get'
  });
}

/**
 * 新增填报模板
 */
export function addFillTemplate(data: Api.Dataplatform.FillTemplateEdit) {
  return request<number>({
    url: '/dataplatform/fill/template',
    method: 'post',
    data
  });
}

/**
 * 更新填报模板
 */
export function updateFillTemplate(data: Api.Dataplatform.FillTemplateEdit) {
  return request({
    url: '/dataplatform/fill/template',
    method: 'put',
    data
  });
}

/**
 * 删除填报模板
 */
export function deleteFillTemplate(ids: number[]) {
  return request({
    url: `/dataplatform/fill/template/${ids.join(',')}`,
    method: 'delete'
  });
}

/**
 * 导出模板
 */
export function exportFillTemplate(id: number, format: string = 'json') {
  return request<Blob>({
    url: `/dataplatform/fill/template/export/${id}`,
    method: 'get',
    params: { format },
    responseType: 'blob'
  });
}

/**
 * 导入模板
 */
export function importFillTemplate(data: Blob, format: string = 'json') {
  return request({
    url: '/dataplatform/fill/template/import',
    method: 'post',
    params: { format },
    data
  });
}

// ==================== 填报数据 ====================

/**
 * 获取填报数据列表
 */
export function fetchFillDataList(params?: Api.Dataplatform.FillDataSearchParams) {
  return request<Api.Dataplatform.FillDataList>({
    url: '/dataplatform/fill/data/list',
    method: 'get',
    params
  });
}

/**
 * 获取填报数据详情
 */
export function fetchFillDataDetail(id: number) {
  return request<Api.Dataplatform.FillData>({
    url: `/dataplatform/fill/data/${id}`,
    method: 'get'
  });
}

/**
 * 新增填报数据
 */
export function addFillData(data: Api.Dataplatform.FillDataEdit) {
  return request<number>({
    url: '/dataplatform/fill/data',
    method: 'post',
    data
  });
}

/**
 * 更新填报数据
 */
export function updateFillData(data: Api.Dataplatform.FillDataEdit) {
  return request({
    url: '/dataplatform/fill/data',
    method: 'put',
    data
  });
}

/**
 * 删除填报数据
 */
export function deleteFillData(ids: number[]) {
  return request({
    url: `/dataplatform/fill/data/${ids.join(',')}`,
    method: 'delete'
  });
}

/**
 * 提交填报数据
 */
export function submitFillData(id: number) {
  return request({
    url: `/dataplatform/fill/data/submit/${id}`,
    method: 'post'
  });
}

/**
 * 审批填报数据
 */
export function approveFillData(id: number, approved: boolean, reason?: string) {
  return request({
    url: `/dataplatform/fill/data/approve/${id}`,
    method: 'post',
    params: { approved, reason }
  });
}

/**
 * 验证填报数据
 */
export function validateFillData(templateId: number, dataContent: string) {
  return request<boolean>({
    url: '/dataplatform/fill/data/validate',
    method: 'post',
    params: { templateId },
    data: dataContent
  });
}

/**
 * 导出填报数据
 */
export function exportFillData(id: number, format: string = 'excel') {
  return request<Blob>({
    url: `/dataplatform/fill/data/export/${id}`,
    method: 'get',
    params: { format },
    responseType: 'blob'
  });
}

/**
 * 导入填报数据
 */
export function importFillData(templateId: number, data: Blob, format: string = 'excel') {
  return request({
    url: '/dataplatform/fill/data/import',
    method: 'post',
    params: { templateId, format },
    data
  });
}
